package com.eastfair.config.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.config.dto.AttachmentDTO;
import com.eastfair.config.dto.AttachmentPageQuery;
import com.eastfair.config.dto.AttachmentSaveDTO;
import com.eastfair.config.dto.AttachmentUpdateDTO;
import com.eastfair.config.entity.Attachment;
import com.eastfair.config.service.AttachmentService;
import com.eastfair.config.vo.AttachmentVO;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 附件资源管理
 * </p>
 *
 * @author zjn
 * @date 2021-08-31
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/attachment")
@Api(value = "Attachment", tags = "附件资源管理")
public class AttachmentController extends SuperController<AttachmentService, Long, Attachment, AttachmentPageQuery, AttachmentSaveDTO, AttachmentUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private AttachmentService attachmentService;

    @Override
    public void handlerResult(IPage<Attachment> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<Attachment> attachmentList = list.stream().map((map) -> {
            Attachment attachment = Attachment.builder().build();
            //TODO 请在这里完成转换
            return attachment;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(attachmentList));
    }

    /**
     * 批量新增附件
     *
     * @param attachmentDTOList 附件列表
     * @return 是否新增成功
     */
    @PostMapping("/saveAttachmentList")
    @ApiOperation(value = "批量新增附件", notes = "批量新增附件")
    public R<Boolean> saveAttachmentList(@RequestBody @Validated({AttachmentDTO.VerifyInsert.class}) List<AttachmentDTO> attachmentDTOList) {
        return R.success(attachmentService.saveAttachmentList(attachmentDTOList));
    }

    /**
     * 批量删除业务附件
     *
     * @param attachmentDTO 业务类型、业务id
     * @return 是否删除成功
     */
    @PostMapping("/deleteByBusiness")
    @ApiOperation(value = "批量删除业务附件", notes = "批量删除业务附件")
    public R<Boolean> deleteByBusiness(@RequestBody @Validated({AttachmentDTO.VerifyUpdate.class}) AttachmentDTO attachmentDTO) {
        return R.success(attachmentService.deleteByBusiness(attachmentDTO));
    }

    /**
     * 根据业务id与业务类型批量删除原附件后批量新增附件
     *
     * @param attachmentDTOList 新附件列表
     * @return 是否操作成功
     */
    @PostMapping("/deleteAndSaveAttachmentList")
    @ApiOperation(value = "根据业务id与业务类型批量删除原附件后批量新增附件", notes = "根据业务id与业务类型批量删除原附件后批量新增附件")
    public R<Boolean> deleteAndSaveAttachmentList(@RequestBody @Validated({AttachmentDTO.VerifyInsert.class}) List<AttachmentDTO> attachmentDTOList) {
        return R.success(attachmentService.deleteAndSaveAttachmentList(attachmentDTOList));
    }

    /**
     * 根据业务id与业务类型查询附件
     *
     * @param attachmentDTO 查询条件
     * @return 附件列表
     */
    @PostMapping("/getAttachmentListByBusiness")
    @ApiOperation(value = "根据业务id与业务类型批量删除原附件后批量新增附件", notes = "根据业务id与业务类型批量删除原附件后批量新增附件")
    public R<List<AttachmentVO>> getAttachmentListByBusiness(@RequestBody @Validated({AttachmentDTO.VerifyQuery.class}) AttachmentDTO attachmentDTO) {
        return R.success(attachmentService.getAttachmentListByBusiness(attachmentDTO));
    }
}
